var UserManager = function () {
	// 初始化数据表格
	var initDataTable = function(){

        var oTable = $('.table').dataTable({
            // set the initial value
            "iDisplayLength": 5,
            "bPaginate": true, //翻页功能
            "bServerSide" : true, // 指定服务器端分页
            "bLengthChange": false, //改变每页显示数据数量
            "bFilter": true, //过滤功能
            "bSort": false, //排序功能
            "bInfo": true,//页脚信息
            "bAutoWidth": true,//自动宽度
            "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
            "bStateSave" : true, // 状态保存
            "sPaginationType": "bootstrap",
            "sAjaxSource" : "/ems/admin/user/getUserList.json", // 数据源
            "oLanguage": {
            	"sUrl" : "/ems/assets/plugins/data-tables/lang/dataTable.cn.txt"
            },
            "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
            	// 删除自带搜索功能
            	$(".dataTables_filter").remove();
            	
                var queryparam = dtUtil.convert_aoData(aoData);
                queryparam.q += "";
                var q = {};
                q.productId = $("#product_id").val();
                q.productName = $("#product_name").val();
                q.skuCode = $("#sku").val();
                q.isCheck = $("#sel_check_status").val();
                q.brandId = $("#sel_brand_name").val();
                queryparam.accessToken = store.get("access_token");
                queryparam.userId = store.get("userId");
                queryparam.mobile = store.get("mobile");
                queryparam.q += JSON.stringify(q);
                oSettings.jqXHR = $.ajax({
                    "dataType" : "json",
                    "contentType" : "application/json;charset=UTF-8",
                    "type" : "GET",
                    "url" : sSource,
                    "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
                    "success" : function(json) {
                    	if(json.code == "2005"){
                    		window.location.href = "login.html";
                    	}else{
                    		dtUtil.successCallback(json, aoData, fnCallback);
                    	}
                    }
                });
            },
            "aoColumns": [{
            	"sTitle" : "用户名",
            	"bSortable" : false,
            	"mData" : "userName"
    		},{
    			"sTitle" : "姓名",
    			"mData" : "realName"
    		},{
    			"sTitle" : "手机号码",
    			"mData" : "mobile"
    		},{
    			"sTitle" : "座机号码",
    			"mData" : "telephone"
    		},{
    			"sTitle" : "邮箱",
    			"mData" : "email"
    		},{
    			"sTitle" : "",
    			"mData" : "id",
    			"mRender" : function(data,type,full){
    				var html = "";
    				if(data == 1){
    					return "";
    				}else{
    					html = '<a href="javascript:;" class="btn default btn-xs green">' 
							+'		<div class="edit"><i class="fa fa-edit"></i>编辑</div>'
							+  '</a>'
							+  '<a href="javascript:;" class="btn default btn-xs black">'
							+      '<div class="delete"><i class="fa fa-trash-o"></i>删除</div>'
							+  '</a>';
    					
    					if(full.status == 1){
    						// 显示禁用
    						html += '<a href="javascript:;" class="btn default btn-xs blue">'
    								+	'<div class="stop"><i class="fa fa-stop"></i>禁用</div>'
    								+'</a>';
    					}else{
    						// 现实启用
    						html +='<a href="javascript:;" class="btn default btn-xs blue">'
    								+	'<div class="play"><i class="fa fa-play"></i>启用</div>'
    								+'</a>';
    					}
    				}
    				return html;
    			}
    		}],
            "aoColumnDefs": [{
                    'bSortable': false,
                    'aTargets': [0]
                }
            ]
        
        });
	};
	
	// 初始化点击事件
	var initClickEvent = function(){
		var tableData = $('.table').dataTable();
		// 编辑按钮
		$(".table").on("click","tr>td>a>div.edit",function(e){
			var rowNode = this.parentNode.parentNode.parentNode;
            var rowData = tableData.fnGetData(rowNode);
            
		});
		// 删除事件
		$(".table").on("click", "tr>td>a>div.delete",function(e){
			var rowNode = this.parentNode.parentNode.parentNode;
            var rowData = tableData.fnGetData(rowNode);
            
            var url = "/ems/admin/user/deleteUser.json";
            var param = {"id" : rowData.id,"userId" : store.get("userId"),"mobile" : store.get("mobile"),"accessToken" : store.get("access_token")};
            ajaxRequest(url,param,"POST",refreshTableData);
		});
		// 启用
		$(".table").on("click","tr>td>a>div.play",function(e){
			var rowNode = this.parentNode.parentNode.parentNode;
            var rowData = tableData.fnGetData(rowNode);
            
            var url = "/ems/admin/user/changeUserStatus.json";
            var param = {"id" : rowData.id,"status" : "1","userId" : store.get("userId"),"mobile" : store.get("mobile"),"accessToken" : store.get("access_token")};
            ajaxRequest(url,param,"POST",refreshTableData);
		});
		// 禁用
		$(".table").on("click","tr>td>a>div.stop",function(e){
			var rowNode = this.parentNode.parentNode.parentNode;
            var rowData = tableData.fnGetData(rowNode);
            
            var url = "/ems/admin/user/changeUserStatus.json";
            var param = {"id" : rowData.id,"status" : "0","userId" : store.get("userId"),"mobile" : store.get("mobile"),"accessToken" : store.get("access_token")};
            ajaxRequest(url,param,"POST",refreshTableData);
		});
	}
	
	// 刷新表格数据
	function refreshTableData(){
		var tableData = $('.table').dataTable();
		tableData.fnFilter();
	}
	
	/**
	 * ajax 请求
	 * url : url 请求地址
	 * param : 参数
	 * fnCallback : 回调函数
	 */
	function ajaxRequest(url , param , methodType, fnCallback){
		$.ajax({
			type : methodType,
			url : url,
			data: param,
			async : false,
			success : function(data){
				if(typeof(fnCallback) != "undefined"){
					bootbox.alert("操作成功");
					fnCallback(data);
				}
				else if(data){
					if(data.code == "000000"){
						bootbox.alert("操作成功");
					}else{
						bootbox.alert("操作失败，" + data.message,1500);
					}
				}else{
					bootbox.alert("数据错误，请刷新页面重试",1500);
				}
			}
		});
	}

    return {

        //main function to initiate the module
        init: function () {
        	initDataTable();
        	initClickEvent();
        }

    };

}();